package com.webank.wedatasphere.qualitis.service.impl;

import com.webank.wedatasphere.qualitis.project.dao.repository.ProjectRepository;
import com.webank.wedatasphere.qualitis.project.entity.Project;
import com.webank.wedatasphere.qualitis.question.constant.OrderStatus;
import com.webank.wedatasphere.qualitis.question.entity.Order;
import com.webank.wedatasphere.qualitis.question.repository.OrderRepository;
import com.webank.wedatasphere.qualitis.response.entity.IndexResponse;
import com.webank.wedatasphere.qualitis.rule.dao.repository.RuleDetailInfoVoRepository;
import com.webank.wedatasphere.qualitis.rule.dao.repository.SqlTaskRepository;
import com.webank.wedatasphere.qualitis.service.IndexStatisticsService;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

@Service
public class IndexStatisticsServiceImpl implements IndexStatisticsService {
    @Resource
    private ProjectRepository projectRepository;

    @Resource
    private SqlTaskRepository sqlTaskRepository;

    @Resource
    private OrderRepository orderRepository;

    @Override
    public IndexResponse count() {
        // 制定标准总数
        long projectCount = projectRepository.count();

        // 质量规则执行总数
        long execCount = sqlTaskRepository.count();

        // 正式标准总数
        Project project = new Project();
        project.setFbzt("已发布");
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withNullHandler(ExampleMatcher.NullHandler.IGNORE)
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase()
                .withIgnoreNullValues();
        Example<Project> example = Example.of(project, matcher);
        long publish = projectRepository.count(example);

        // 问题总数
        long issue = orderRepository.count();

        // 已解决问题总数
        long complete = orderRepository.count(completeOrderCountSpec());

        // 待我解决问题总数
        // TODO 根据当前用户过滤
        long todo = orderRepository.count(myTodoOrderCountSpec(1L));

        return new IndexResponse(execCount, issue, todo, complete, projectCount, publish);
    }

    private Specification<Order> completeOrderCountSpec() {
        return (root, query, builder) -> builder.and(builder.equal(root.get("status"), OrderStatus.RESOLVED.getCode()));
    }

    private Specification<Order> myTodoOrderCountSpec(Long handler) {
        return (root, query, builder) -> {
            List<Predicate> predicates = new ArrayList<>();
            predicates.add(builder.equal(root.get("handler"), handler));
            predicates.add(root.get("status")
                    .in(OrderStatus.NOT_RESOLVE.getCode(),
                            OrderStatus.RESOLVING.getCode(),
                            OrderStatus.RETURN.getCode()));
            return builder.and(predicates.toArray(new Predicate[0]));
        };
    }
}
